package com.manlitech.cloudboot.shardingjdbc.controller;

/**
 * @author shensg
 * @date 2021/7/21 13:37
 */
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manlitech.cloudboot.common.utils.Result;
import com.manlitech.cloudboot.common.utils.ResultUtil;
import com.manlitech.cloudboot.shardingjdbc.mapper.UserInfoMapper;
import com.manlitech.cloudboot.shardingjdbc.pojo.UserInfo;
import com.manlitech.cloudboot.shardingjdbc.service.UserInfoOtherServiceImpl;
import com.manlitech.cloudboot.shardingjdbc.service.UserInfoServiceImpl;
import com.manlitech.cloudboot.shardingjdbc.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserInfoController
 * @Description UserInfo接口请求层
 * @Author Z咸鱼有梦
 * @Date 2019/12/02 0025 15:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/user")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoServiceImpl userInfoService;

    @Autowired
    private UserInfoOtherServiceImpl userInfoOtherService;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 保存用户信息 正常保存
     * @return
     */
    @PostMapping("/saveUserSuccess")
    public String saveUserSuccess(@RequestBody UserInfoVo userInfoVo){
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVo,userInfo);
        userInfoService.save(userInfo);
        return "SUCCESS";
    }

    /**
     * 保存用户信息 报错回滚
     * @return
     */
    @PostMapping("/saveUserError")
    public String saveUser(@RequestBody UserInfoVo userInfoVo){
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVo,userInfo);
        userInfoOtherService.saveOther(userInfo);
        return "SUCCESS";
    }

    /**
     * 获取所有用户
     * @return
     */
    @PostMapping("/findUserSuccess")
    public Result findUserSuccess(){
        //IPage<UserInfo> testPage = new Page<>(1, 2);
        // testPage = userInfoService.lambdaQuery().page(testPage);
        //return ResultUtil.success(testPage.getRecords());
        return ResultUtil.success(userInfoService.findUserSuccess());
    }

    /**
     * 获取所有用户
     * @return
     */
    @PostMapping("/findAllUserSuccess")
    public Result findAllUserSuccess(){
        return ResultUtil.success(userInfoService.list());
    }

    /**
     * 获取所有用户 验证密文列 name
     * 通过名文查询加密的列
     * query:
     *         with:
     *           cipher:
     *             column: true #是否使用密文列查询
     * @return
     */
    @PostMapping("/findByName")
    public Result findByName(){
        return ResultUtil.success(userInfoService.findByName());
    }
}

